﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;
using Models;

namespace 包覆门.OtherDecManager
{
    public partial class OtherDetailEditing : Form
    {
        public bool IsNew { get; set; }
        public string EditingId { get; set; }
        public string ParentId { get; set; }
        public string mxbhid { get; set; }
        private OtherDecDAL dal = new OtherDecDAL();
        public OtherDetailEditing()
        {
            InitializeComponent();
        }
        //加载
        private void OtherDetailEditing_Load(object sender, EventArgs e)
        {
            this.cbOthermain.DisplayMember = "decname";
            this.cbOthermain.ValueMember = "oid";
            DataTable dt = dal.GetMainList();
            dt.Rows.InsertAt(dt.NewRow(), 0);
            this.cbOthermain.DataSource = dt;
            if (IsNew)
            {
                this.tbMxbh.Text = mxbhid;
                this.cbOthermain.SelectedValue = ParentId;
            }
            else
            {
                LoadData();
            }
        }
        //加载明细数据
        public void LoadData()
        {
            OtherDetailDec dec = dal.GetOtherDetailDecById(EditingId);
            this.cbOthermain.SelectedValue = dec.CDecParentId;
            this.tbMxbh.Text = dec.CMxbhId;
            this.tbDecname.Text = dec.CDecName;
            this.tbLimitMx.Text = dec.CLimitMx;


        }
        //保存数据
        private void btnSave_Click(object sender, EventArgs e)
        {
            OtherDetailDec dec = new OtherDetailDec();
            dec.CDecParentId = cbOthermain.SelectedValue == null ? "" : cbOthermain.SelectedValue.ToString();
            dec.CDecParentName = cbOthermain.Text;
            dec.CDecName = tbDecname.Text;
            dec.CLimitMx = tbLimitMx.Text;
            dec.CMxbhId = tbMxbh.Text;

            if (!ValidateData(dec)) return;
            if (IsNew)
            {
                int result = dal.SaveDetailOtherDec(dec);
                if (result == 1)
                {
                    MessageBox.Show("保存成功");
                    DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("保存失败");
                    DialogResult = DialogResult.No;
                }
            }
            else
            {
                dec.Cid = Convert.ToInt32(EditingId);
                int result = dal.UpdateDetailOtherDec(dec);
                if (result == 1)
                {
                    MessageBox.Show("保存成功");
                    DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("保存失败");
                    DialogResult = DialogResult.No;
                }
            }
        }
        //校验数据
        private bool ValidateData(OtherDetailDec dec)
        {
            bool flag = true;
            if (string.IsNullOrWhiteSpace(dec.CDecParentId))
            {
                MessageBox.Show("请选择父类选项");
                return false;
            }
            if (string.IsNullOrWhiteSpace(dec.CDecName))
            {
                MessageBox.Show("下拉选项不能为空");
                return false;
            }

            return flag;
        }
        //取消
        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.No;
        }
    }
}
